<?php
/**
 * 一开始写的代码 有些混乱 暂不改造 2014-05-08  
 * 
 * 
 */
class CateController extends NodeController
{
    /**
     * json格式
     * 默认1为展开状态
     */
    public function ztree ($type = null, $ids = NULL, $rootid = 1)
    {
        if ($_REQUEST['id']) {
            $fid = empty($_REQUEST['id']) ? 0 : trim($_REQUEST['id']);
            $rootid = trim($_REQUEST['id']);
            $rs = CategoryModel::instance()->getCategory($fid);
             //$rs = CategoryModel::instance()->getcatagory($fid);
        } else {
            //  $rs = CategoryModel::instance()->getcatagory(2014);
            $rs = CategoryModel::instance()->getCategory(1, 1, "ZTREE");
        }
        foreach ($rs as $k => $v) {
            //找一个/*
            $fcate = CategoryModel::instance()->getCategory($v['id'], 1);
            if (! empty($fcate)) {
                $rs[$k]['isParent'] = true;
                 //   $rs[$k]['open'] = $v['Deep'] > 1 ? false : true;
            } else {
                $rs[$k]['isParent'] = false;
            }
            //   unset($rs[$k]['fid']);
            $rs[$k]['open'] = $v['id'] == 1 ? true : false;
            $rs[$k]['name'] = $v['name'] . $v['id'];
        }
        $json = json_encode($rs);
        if ($type == "add") {
            return $json;
            exit();
        }
        echo $json;
    }
    /**
     * 分类专题页面 如果没有 就用列表页面
     */
    public function bind ()
    {
        if ($_POST['model'] == 'bind') {
            $rs = self::getNewsService()->bindCateTemp($_POST['type'], 
            $_POST['name'], $_POST['category']);
            $this->sendNotice(SUCCESS, $rs['id'], true);
        }
    }
    public function bindField ()
    {
        if ($_POST['cid'] < 1) {
            echo '你提交的分类不正确';
            return;
        }
        self::getNewsService()->bindField($_POST['cid'], $_POST['eid']);
        /* 删除原有配置 */
        echo "SUCCESS";
        return true;
    }
    public static function getNewsService ()
    {
        return new NewsService($_REQUEST['cid'], $_REQUEST['mid']);
    }
    /**
     * 分类绑定字段
     */
    public function bindFieldView ()
    {
        $categoryid = isset($_GET['cid']) ? $_GET['cid'] : 0;
        $category = self::getNewsService()->getCategory($categoryid, null);
        $extend = self::getNewsService()->getCateField($categoryid);
        $this->view()->assign('category', $category);
        $this->view()->assign('extend', $extend);
        $this->view()->display('file:news/cate.extend.tpl');
    }
    /**
     * 获取分类信息
     * 
     */
    public function category ()
    {
        $rs = self::getNewsService()->getCate($_POST['cid'], $this->_group);
        $this->sendNotice($rs);
    }
    public function getCategory ($categoryid)
    {
        return CategoryModel::instance()->getcatagory($categoryid);
    }
    // 更新新闻权限
    public function saveCate ()
    {
        if ($_POST) {
            if (isset($_POST['jicheng'])) {
                $category = CategoryModel::instance()->getCateogryById($_POST['id']);
                $sonCate = CategoryModel::instance()->getcatagory($_POST['id']);
                //  $cids = explode("-", $category['topid']);
                foreach ($sonCate as $v) {
                    CategoryModel::instance()->saveCategoryKeys(
                    array('groupid' => $_POST['groupid']), 
                    array('id' => $v['id']));
                }
            }
            CategoryModel::instance()->saveCategoryKeys(
            array('groupid' => $_POST['groupid']), 
            array('id' => $_POST['id']));
            $this->sendNotice(SUCCESS);
            exit();
        }
    }
    public function move ()
    {
        if ($_POST['id'] > 0 && $_POST['fid'] > 0) {
            CategoryModel::instance()->movecatagory($_POST['id'], $_POST['fid']);
            $this->sendNotice(SUCCESS, NULL, true);
            exit();
        } else {
            $this->sendNotice("can't find parentid", NULL, false);
        }
    }
    /**
     * 添加分类
     */
    public function editCategory ()
    {
        if (empty($_POST)) {
            $this->view()->display('file:news/category.tpl');
            exit();
        }
        // NewsModel::instance()->getCategory()
        if (isset($_GET['fid']) && $_GET['fid'] > 0) {
            $fid = $_GET['fid'];
        } else {
            $fid = 1;
        }
        //$this->ztree();
        $cate = $this->getCategory($fid, NULL);
        $cateInfo = CategoryModel::instance()->getCategory();
        $extend = ExtendModel::instance()->getExtend(
        array('module' => 'news'));
        $this->view()->assign('extend', $extend);
        $this->view()->assign('cateinfo', $cateInfo);
        $this->view()->assign('category', $cate);
        if ($_POST['type'] == 'add' && $_POST['name'] != '') {
            $lastid = CategoryModel::instance()->addsort($_POST['category'], 
            $_POST['name'], $_POST['mid']);
            $this->sendNotice(SUCCESS, 
            array('id' => $lastid));
        } else 
            if ($_POST['type'] == 'del') {
                $cateNum = CategoryModel::instance()->checkCateNum(
                array('fid' => $_POST['category']));
                if ($cateNum > 0) {
                    $this->sendNotice(FAILED, '/news/editcategory', 2);
                    exit();
                } else {
                    CategoryModel::instance()->movecatagory($_POST['category'], 
                    1067);
                    $this->sendNotice(SUCCESS);
                }
            } else 
                if ($_POST['type'] == 'rename' && $_POST['name'] != '') {
                    CategoryModel::instance()->renameCategory(
                    array('name' => $_POST['name']), 
                    array('id' => $_POST['category']));
                    $this->sendNotice(SUCCESS);
                } else {
                    $category = CategoryModel::instance()->getCateogryById(2014);
                    $this->view()->assign('category', $category);
                    $this->view()->clearCache('file:news/category.tpl');
                    $this->view()->display('file:news/category.tpl');
                }
    }
}